package com.xjl.javacore.code.ioexample.bioexample;

import lombok.SneakyThrows;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class Handler extends Thread {
    //1.建立客户端socket连接，指定服务器位置及端口
        private Socket socket;
        Handler(Socket socket) {
            this.socket = socket;
        }
        @SneakyThrows
        @Override
        public void run() {
                //2.得到socket读写流
                OutputStream os=socket.getOutputStream();
                PrintWriter pw=new PrintWriter(os);
                //输入流
                InputStream is=socket.getInputStream();
                BufferedReader br=new BufferedReader(new InputStreamReader(is));
                //3.利用流按照一定的操作，对socket进行读写操作
                String info="这是客户端的内容";
                pw.write(info);
                pw.flush();
        }

    public static void main(String[] args) throws Exception {
        try (Socket client = new Socket(InetAddress.getLocalHost(), 8080)) {
            BufferedReader buferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            buferedReader.lines().forEach(s -> System.out.println(s));
        }
    }
    }